package com.bds.btcdc.adapter.gateio;

import com.bds.btcdc.model.CoinPairExchanges;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.simp.stomp.StompSession;

import com.alibaba.fastjson.JSONObject;
import com.bds.btcdc.adapter.gateio.service.GateioService;
import com.bds.btcdc.api.model.Result;
import com.bds.btcdc.util.Const;
import com.bds.btcdc.util.SpringContextHolder;

import java.util.List;

public class GateioTask extends Thread {
	private Logger logger = LoggerFactory.getLogger(getClass());

	private GateioService service = SpringContextHolder.getBean(GateioService.class);

	private StompSession session;
	private List<CoinPairExchanges> giteio_pairs;

	public GateioTask(StompSession session, List<CoinPairExchanges> giteio_pairs) {
		this.session = session;
		this.giteio_pairs = giteio_pairs;
	}

	public synchronized void gateioTask() {
		try {
			Result data = service.getMarketInfo(giteio_pairs);
			if (data != null) {
				String msg = JSONObject.toJSONString(data);
				if (session != null)
					// 推到btc-web主题
					session.send(Const.GATEIO.MARKET_TOPIC, msg.getBytes());
			}
		} catch (Exception e) {
			logger.info("gateio 连接平台websocket服务,推送数据发生异常！");
			logger.error("", e);
		}

	}

	@Override
	public synchronized void run() {
		try {
			this.gateioTask();
		} catch (Exception e) {
			logger.error("", e);
		}

	}

}
